package com.feige.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feige.pojo.Blog;
import com.feige.pojo.vo.BlogQueryVo;
import com.feige.result.AjaxResult;
import com.feige.service.BlogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-09-25
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;
    

    @ApiOperation(value = "获取博客分页列表")
    @PostMapping("/list")
    public AjaxResult index(@RequestBody BlogQueryVo blogQueryVo){
        Page<Blog> pageParam = new Page<>(blogQueryVo.getPageNum(), blogQueryVo.getPageSize());
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(blogQueryVo.getId())) {
            wrapper.eq("id",blogQueryVo.getId());
        }
        if(!StringUtils.isEmpty(blogQueryVo.getModule())) {
            wrapper.like("module",blogQueryVo.getModule());
        }
        if(!StringUtils.isEmpty(blogQueryVo.getTitle())) {
            wrapper.like("title",blogQueryVo.getTitle());
        }
        if (!StringUtils.isEmpty(blogQueryVo.getStatus())){
            wrapper.eq("status",blogQueryVo.getStatus());
        }
        if (!StringUtils.isEmpty(blogQueryVo.getBeginTime())){
            wrapper.ge("gmt_create",blogQueryVo.getBeginTime());
        }
        if (!StringUtils.isEmpty(blogQueryVo.getEndTime())){
            wrapper.le("gmt_create",blogQueryVo.getEndTime());
        }
        blogService.page(pageParam, wrapper);
        AjaxResult success = AjaxResult.success(pageParam.getRecords());
        success.put("total",pageParam.getTotal());
        return success;
    }


    @ApiOperation(value = "获取博客")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable String id) {
        Blog Blog = blogService.getById(id);
        return AjaxResult.success(Blog);
    }

    @ApiOperation(value = "新增博客")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Blog blog) {
        boolean save = blogService.save(blog);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "修改博客和权限")
    @PutMapping("/update")
    public AjaxResult updateById(@RequestBody Blog blog) {
        boolean update = blogService.updateById(blog);
        if (update){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "删除博客")
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable String id) {
        boolean remove = blogService.removeById(id);
        if (remove){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据id列表删除博客")
    @DeleteMapping("/batchRemove")
    public AjaxResult batchRemove(@RequestBody List<String> idList) {
        boolean remove = blogService.removeByIds(idList);
        if (remove) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

}

